import api from '../api'

function actionSuccess (commit) {
  return msg => commit('SET_SUCCESS_MSG', { msg })
}

export default {
  // 礼包列表
  getGiftList ({ commit }, params = {}) {
    api.getGiftList(params, data => {
      if (params.is_app === 1) {
        commit('GET_APP_GIFT_LIST', { data })
      } else {
        commit('GET_H5_GIFT_LIST', { data })
      }
    })
  },

  // 玩家礼包列表
  getUserGiftList ({ commit }, params = {}) {
    api.getUserGiftList(params, data => {
      commit('SET_USER_GIFT_LIST', { data })
    })
  },

  // 礼包详情
  getGiftDetail ({ commit }, params) {
    api.getGiftDetail(params, data => {
      commit('GET_GIFT_DETAIL', { data })
    })
  },

  // 领取礼包
  addGift ({ commit }, params) {
    api.addGift(params, data => {
      commit('GET_GIFT_DETAIL', { data })
    })
  },

  // 游戏列表
  getGameList ({ commit }, params) {
    api.getGameList(params, data => {
      commit('GET_GAME_LIST', { data })
    })
  },
  searchGameList ({ commit }, params) {
    api.searchGameList(params, data => {
      commit('GET_GAME_LIST', { data })
    })
  },

  // 开服游戏列表
  getServerList ({ commit }, params) {
    api.getServerList(params, data => {
      switch (params.server_type) {
        case 1:
          commit('SET_CURR_SERVER_LIST', { data })
          break
        case 2:
          commit('SET_NEXT_SERVER_LIST', { data })
          break
        case 3:
          commit('SET_PREV_SERVER_LIST', { data })
          break
      }
    })
  },

  // 首页
  getPlayIndex ({ commit }) {
    api.getPlayIndex(data => {
      commit('GET_PLAY_INDEX', { data })
    })
  },

  // 游戏详情
  getGameDetail ({ commit }, params) {
    api.getGameDetail(params, data => {
      commit('GET_GAME_DETAIL', { data })
    })
  },

  // 获取用户信息
  getUserinfo ({ commit, dispatch }) {
    api.getUserinfo(data => {
      commit('GET_USERINFO', { data })

      if (data.mem_id) {
        /* global HuoUtils */
        HuoUtils.switchTemplate()
      }
    })
  },

  // 消息列表
  getMessageList ({ commit }, params) {
    api.getMessageList(params, data => {
      commit('GET_MESSAGE_LIST', { data })
    })
  },

  // 平台消费记录列表
  getPtbConsumeList ({ commit }, params) {
    api.getPtbConsumeList(params, data => {
      commit('SET_PTB_CONSUME_LIST', { data })
    })
  },

  /*************************************************************
   *
   * 旧接口，开发完删除
   *
   *************************************************************/
  // 获取分享内容
  getShareDetail ({ commit }, params) {
    api.getShareDetail(params, data => {
      commit('GET_SHARE_DETAIL', { data })
    })
  },

  // 一起玩游戏列表
  getPlayList ({ commit }) {
    api.getPlayList(data => {
      commit('GET_PLAY_LIST', { data })
    })
  },

  // 一起玩游戏详情
  getPlayDetail ({ commit }, params) {
    api.getPlayDetail(params, data => {
      commit('GET_PLAY_DETAIL', { data })
    })
  },

  // 单栏目游戏列表
  getTagGameList ({ commit }, params) {
    api.getTagGameList(params, data => {
      commit('GET_TAG_GAME_LIST', { data })
    })
  },

  // 我玩过的游戏列表
  getPlayedList ({ commit }, params) {
    api.getPlayedList(params, data => {
      commit('GET_PLAYED_LIST', { data })
    })
  },

  // 精选游戏列表
  getSelectList ({ commit }, params) {
    api.getSelectList(params, data => {
      commit('GET_SELECT_LIST', { data })
    })
  },

  // 任务主页信息
  getTaskIndex ({ commit }) {
    api.getTaskIndex(data => {
      commit('GET_TASK_INDEX', { data })
    })
  },

  // 领取任务奖励
  completeTask ({ commit, dispatch }, params) {
    api.completeTask(params, data => {
      commit('COMPLETE_TASK', { data })

      // 如果是红包任务则刷新红包列表
      if (params.agt_type === 2) {
        dispatch('getPackageList')
      }
    })
  },

  // 可领取的红包
  getPackageList ({ commit }) {
    api.getPackageList(data => {
      commit('GET_PACKAGE_LIST', { data })
    })
  },

  // 金币抽奖详情
  getLotteryDetail ({ commit }) {
    api.getLotteryDetail(data => {
      commit('GET_LOTTERY_DETAIL', { data })
    })
  },

  // 金币抽奖
  drawLottery ({ commit }) {
    api.drawLottery(data => {
      commit('DRAW_LOTTERY', { data })
    })
  },

  // 绑定手机
  bindMobile ({ commit }, params) {
    api.bindMobile(params, actionSuccess(commit))
  },

  // 校验原有手机
  checkMobile ({ commit }, params) {
    api.checkMobile(params, actionSuccess(commit))
  },

  // 解绑第三方信息
  unbindOauth ({ commit }, params) {
    api.unbindOauth(params, actionSuccess(commit))
  },

  // 绑定第三方信息
  bindOauth ({ commit }, params) {
    api.bindOauth(params, actionSuccess(commit))
  },

  // 获取第三方信息
  getOauthInfo ({ commit }) {
    api.getOauthInfo(data => {
      commit('GET_OAUTH_INFO', { data })
    })
  },

  // 设置实名认证
  setIdentify ({ commit }, params) {
    api.setIdentify(params, actionSuccess(commit))
  },

  // 成员列表
  getMemList ({ commit }, params) {
    api.getMemList(params, data => {
      commit('GET_MEM_LIST', { data })
    })
  },

  // 收益列表
  getIncomeList ({ commit }, params) {
    api.getIncomeList(params, data => {
      commit('GET_INCOME_LIST', { data })
    })
  },

  // 申请提现
  withdraw ({ commit }, params) {
    api.withdraw(params, actionSuccess(commit))
  },

  // 提现配置
  getWithdrawConf ({ commit }) {
    api.getWithdrawConf(data => {
      commit('GET_WITHDRAW_CONF', { data })
    })
  },

  // 提现明细
  getWithdrawList ({ commit }, params) {
    api.getWithdrawList(params, data => {
      commit('GET_WITHDRAW_LIST', { data })
    })
  },

  // 平台币充值配置
  getRechargeConf ({ commit }) {
    api.getRechargeConf(data => {
      commit('GET_RECHARGE_CONF', { data })
    })
  },

  // 平台币充值-预下单
  recharge ({ commit }, params) {
    api.recharge(params, data => {
      commit('RECHARGE', { data, type: params.payway })
    })
  },

  // 平台币充值记录
  getRechargeList ({ commit }, params) {
    api.getRechargeList(params, data => {
      commit('GET_RECHARGE_LIST', { data })
    })
  },

  // 平台币充值结果查询
  queryOrder ({ commit }, params) {
    api.queryOrder(params, data => {
      commit('QUERY_ORDER', { data })
    })
  },

  // 金币收支明细
  getIntegralList ({ commit }, params) {
    api.getIntegralList(params, data => {
      commit('GET_INTEGRAL_LIST', { data })
    })
  },

  // 红包收支明细
  getRedpacketList ({ commit }, params) {
    api.getRedpacketList(params, data => {
      commit('GET_REDPACKET_LIST', { data })
    })
  },

  // 收益排行列表
  getIncomeRankList ({ commit }, params) {
    api.getIncomeRankList(params, data => {
      if (params.range === 'week') {
        commit('GET_WEEK_RANK_LIST', { data })
      } else if (params.range === 'month') {
        commit('GET_MONTH_RANK_LIST', { data })
      } else {
        commit('GET_ALL_RANK_LIST', { data })
      }
    })
  },

  // 手机号登陆或注册
  login ({ commit }, params) {
    api.login(params, actionSuccess(commit))
  },

  // 发送验证码
  sendSms ({ commit }, params) {
    api.sendSms(params, actionSuccess(commit))
  },

  // 第三方登陆入口
  loginOauth ({ commit }, params) {
    api.loginOauth(params, actionSuccess(commit))
  },

  // 常见问题
  getServiceInfo ({ commit }, params) {
    api.getServiceInfo(params, data => {
      commit('GET_SERVICE_INFO', { data })
    })
  },

  // 第三方授权配置
  getOauthConf ({ commit }) {
    api.getOauthConf(data => {
      commit('SET_OAUTH_CONF', { data })
    })
  },

  // 保存授权信息
  saveOauthInfo ({ commit }, params) {
    api.saveOauthInfo(params, data => {
      commit('SET_USERINFO', { data })
      window.location.reload()
    })
  }
}
